home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pinstaller / runtimedeps.py < prev    next >
Text File  |  2005-12-26  |  4KB  |  131 lines

  1. """
  2. # Copyright 1999-2005 Gentoo Foundation
  3. # This source code is distributed under the terms of version 2 of the GNU
  4. # General Public License as published by the Free Software Foundation, a copy
  5. # of which can be found in the main directory of this project.
  6. Gentoo Linux Installer
  7.  
  8. $Id: runtimedeps.py,v 1.3 2005/12/26 19:26:26 agaffney Exp $
  9. """
  10.  
  11. import os, sys
  12.  
  13. class MissingPackagesError(Exception):
  14.     pass
  15.  
  16. class depgraph:
  17.  
  18.     def __init__(self):
  19.         self.graph = {}
  20.     
  21.     def add(self, node, parent=None):
  22.         if node not in self.graph:
  23.             self.graph[node] = [[], []]
  24.         if parent and parent not in self.graph[node][0]:
  25.             if parent not in self.graph:
  26.                 self.graph[parent] = [[], []]
  27.             self.graph[node][0].append(parent)
  28.             self.graph[parent][1].append(node)
  29.  
  30.     def remove(self, node):
  31.         for parent in self.graph[node][0]:
  32.             self.graph[parent][1].remove(node)
  33.         for child in self.graph[node][1]:
  34.             self.graph[child][0].remove(node)
  35.         return self.graph.pop(node)
  36.     
  37.     def has_node(self, node):
  38.         return node in self.graph
  39.     
  40.     def leaf_nodes(self):
  41.         return [node for node in self.graph if not self.graph[node][1]]
  42.     
  43.     def node_count(self):
  44.         return len(self.graph)
  45.     
  46.     def important_node(self):
  47.         important_node = None
  48.         importance = 0
  49.         for node in self.graph:
  50.             if len(self.graph[node][0]) > importance:
  51.                 importance = len(self.graph[node][0])
  52.                 important_node = node
  53.         return important_node
  54.  
  55. def resolve_deps(dep_list):
  56.     if dep_list and dep_list[0] == "||":
  57.         for dep in dep_list[1:]:
  58.             if isinstance(dep, list):
  59.                 try:
  60.                     atoms = resolve_deps(dep)
  61.                 except MissingPackagesError:
  62.                     continue
  63.             else:
  64.                 atoms = [dep]
  65.             all_found = True
  66.             for atom in atoms:
  67.                 if not atom.startswith("!") and not vdb.match(atom):
  68.                     all_found = False
  69.                     break
  70.             if all_found:
  71.                 return atoms
  72.         raise MissingPackagesError(dep_list)
  73.     atoms = []
  74.     for dep in dep_list:
  75.         if isinstance(dep, list):
  76.             atoms.extend(resolve_deps(dep))
  77.         elif not dep.startswith("!"):
  78.             if not vdb.match(dep):
  79.                 raise MissingPackagesError([dep])
  80.             atoms.append(dep)
  81.     return atoms
  82.  
  83. def calc_required_pkgs(atom, graph, parent=None):
  84.     pkg = portage.best(vdb.match(atom))
  85.     if not pkg:
  86.         raise MissingPackagesError([atom])
  87.     if pkg == parent:
  88.         return
  89.     already_processed = graph.has_node(pkg)
  90.     graph.add(pkg, parent)
  91.     if already_processed:
  92.         return
  93.     useflags = vdb.aux_get(pkg, ["USE"])[0].split()
  94.     rdep_raw = " ".join(vdb.aux_get(pkg, ["RDEPEND", "PDEPEND"]))
  95.     rdep_struct = portage_dep.use_reduce(portage_dep.paren_reduce(rdep_raw), uselist=useflags)
  96.     rdep_struct = portage.dep_virtual(portage_dep.dep_opconvert(rdep_struct), portage.settings)
  97.     rdep_atoms = portage.unique_array(resolve_deps(rdep_struct))
  98.     for atom in rdep_atoms:
  99.         calc_required_pkgs(atom, graph, pkg)
  100.  
  101. def prune_existing(graph):
  102.     db = portage.db[portage.root]["vartree"].dbapi
  103.     for atom in db.cp_all():
  104.         for pkg in db.match(atom):
  105.             if graph.has_node(pkg):
  106.                 graph.remove(pkg)
  107.  
  108. def get_deps(pkgs):
  109.         pkglist = []
  110.         graph = depgraph()
  111.         for pkg in pkgs.split():
  112.             if not vdb.match(pkg):
  113.                 continue
  114.             calc_required_pkgs(pkg, graph)
  115.         while graph.node_count():
  116.             leaf_nodes = graph.leaf_nodes()
  117.             if not leaf_nodes:
  118.                 node = graph.important_node()
  119.                 pkglist.append(node)
  120.                 graph.remove(node)
  121.                 continue
  122.             pkglist.extend(leaf_nodes)
  123.             for node in leaf_nodes:
  124.                 graph.remove(node)
  125.         return pkglist
  126.  
  127. os.environ['ROOT'] = sys.argv[1]
  128. import portage, portage_dep
  129. vdb = portage.db["/"]["vartree"].dbapi
  130. print "\n".join(get_deps(" ".join(sys.argv[2:])))
  131.